import Taro from "@tarojs/taro";
import config from '../config/domian'
// import config from "../config/domian"
// const config = require('./../config/domian')
import allExtrance from './../config/allExtrance'

const Public = {
    Person(json) {
        let url = config[process.env.NODE_ENV].hostSaas + '/worker/person'
        let authorization = ''
        let avoidAuthorization = 'false'
        let domain = config[process.env.NODE_ENV].domain
        if (Taro.getStorageSync('Authorization')) {
            authorization = Taro.getStorageSync('Authorization')
        } else {
            avoidAuthorization = 'true'
        }
        return new Promise((resolve, reject) => {
            Taro.request({
                url: url,
                data: json,
                method: 'POST',
                // mode: 'no-cors',
                // dataType:'String',
                header: {
                    'domain': domain,
                    'Authorization': authorization,
                    'avoidAuthorization': avoidAuthorization,
                    'httpVersion': config.version,
                    // 'content-type': 'application/json'
                }
            }).then(res => {
                console.log(res)
                let { data: { data: { banner,version, saas, shopUser, user, worker, saasConfig,h5user,jobStarCount,workerApplyCount } } } = res
                let { data: { data } } = res

                //平台处理
                allExtrance.util.Taro.setStorageSync('logo', saas.logo)   //平台logo
                allExtrance.util.Taro.setStorageSync('slogan', saas.slogan)     //平台口号
                allExtrance.util.Taro.setStorageSync('name', saas.name)    //平台名称
                allExtrance.util.Taro.setStorageSync('miniprogramImg', saas.miniprogramImg)    //平台头像
                allExtrance.util.Taro.setStorageSync('user', user)
                allExtrance.util.Taro.setStorageSync('worker', worker)
                allExtrance.util.Taro.setStorageSync('saasConfig', saasConfig)
                allExtrance.util.Taro.setStorageSync('themeColor', '#ffdd00')
                allExtrance.util.Taro.setStorageSync('consumerIndexBanner', banner)
                shopUser && allExtrance.util.Taro.setStorageSync('shopUser', shopUser)
                shopUser && allExtrance.util.Taro.setStorageSync('company', shopUser.company_id)
                allExtrance.util.Taro.setStorageSync('user_saas_id', user.user_saas_id)
                if(Taro.getEnv() === Taro.ENV_TYPE.WEAPP){
                    if(user && user.mp_openid != null){
                        allExtrance.util.Taro.setStorageSync('hasWechatUser', true)
                    }else{
                        allExtrance.util.Taro.setStorageSync('hasWechatUser', false)
                    }
                }else if(Taro.getEnv() === Taro.ENV_TYPE.WEB){
                    if(user && user.oa_openid != ''){
                        allExtrance.util.Taro.setStorageSync('hasWechatUser', true)
                    }else{
                        allExtrance.util.Taro.setStorageSync('hasWechatUser', false)
                    }
                }

                resolve(data)
            })
        })
    },
    ShareConfig(json) {
        let url = config[process.env.NODE_ENV].hostSaas + '/wechat/webEmpower/jsConfig'
        let domain = config[process.env.NODE_ENV].domain
        let formJson = {
            domain: domain,
            // url: config[process.env.NODE_ENV].hostSaas
            // redirectUrl: config[process.env.NODE_ENV].hostSaas + '/#' + json.url
        }
        console.log(formJson, 'formJson')
        return new Promise((resolve, reject) => {
            Taro.request({
                url: url,
                data: formJson,
                method: 'POST',
                header: {
                    domain: domain,
                    'content-type': 'application/x-www-form-urlencoded'
                }
            }).then(res => {
                if (res.statusCode == 200) {
                    resolve(res.data.data)
                }
            }).catch(err => {
                reject(err)
            })
        })
    },
    Theme(json) {
        let url = config[process.env.NODE_ENV].hostSaas + '/theme'
        let domain = config[process.env.NODE_ENV].domain
        let formJson = {
            domain: (Taro.getEnv() === Taro.ENV_TYPE.WEB) ? domain : '',
            // url: config[process.env.NODE_ENV].hostSaas
            // redirectUrl: config[process.env.NODE_ENV].hostSaas + '/#' + json.url
        }
        return new Promise((resolve, reject) => {
            Taro.request({
                url: url,
                data: formJson,
                method: 'POST',
                header: {
                    domain: domain,
                    'content-type': 'application/x-www-form-urlencoded'
                }
            }).then(res => {
                if (res.statusCode == 200) {
                    resolve(res.data.data)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }
}

export default Public;

